<?php

/**
 * This is the model class for table "{{photos}}".
 *
 * The followings are the available columns in table '{{photos}}':
 * @property integer $id
 * @property integer $album_id
 * @property string $name
 */
class Photos extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Photos the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{photos}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('album_id, crdate', 'required'),
			array('album_id', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>1000),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, album_id, name, crdate', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user'=>array(self::BELONGS_TO, 'Users', 'user_id'),
			'album'=>array(self::BELONGS_TO, 'Albums', 'album_id'),
			'comments'=>array(self::HAS_MANY, 'PhotosComments', 'photo_id',
				'order' => 'comments.id ASC',
				'condition'=>'comments.parent_id=0'
			),
			'countComments'=>array(self::STAT, 'PhotosComments', 'photo_id'),
			'likes'=>array(self::HAS_MANY, 'PhotosLikes', 'photo_id'),
			'countLikes'=>array(self::STAT, 'PhotosLikes', 'photo_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'album_id' => 'Album',
			'name' => 'Name',
			'crdate' => 'Дата создания',
		);
	}
	
	protected function beforeDelete()
	{
		if(parent::beforeDelete())
		{
			PhotosComments::model()->deleteAll(
				'photo_id=:photo_id',
				array(':photo_id'=>$this->id)
			);
			PhotosLikes::model()->deleteAll(
				'photo_id=:photo_id',
				array(':photo_id'=>$this->id)
			);
			return true;
		}
		else
			return false;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('album_id',$this->album_id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('crdate',$this->crdate,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}